package com.briup.smartcity.service.impl;

import com.briup.smartcity.bean.Article;
import com.briup.smartcity.bean.BaseUser;
import com.briup.smartcity.bean.Category;
import com.briup.smartcity.bean.extend.ArticleExtend;
import com.briup.smartcity.exception.ServiceException;
import com.briup.smartcity.mapper.ArticleMapper;
import com.briup.smartcity.mapper.BaseUserMapper;
import com.briup.smartcity.mapper.CategoryMapper;
import com.briup.smartcity.mapper.extend.ArticleExtendMapper;
import com.briup.smartcity.service.IArticleService;
import com.briup.smartcity.utils.Result;
import com.briup.smartcity.utils.ResultCode;
import com.briup.smartcity.utils.ResultUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements IArticleService {
    @Autowired
    private ArticleExtendMapper extendMapper;
    @Autowired
    private ArticleMapper mapper;
    @Autowired
    private BaseUserMapper userMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    //通过id查询咨询信息，同时获取咨询对应的栏目和用户信息
    @Override
    public ArticleExtend findByIdWithCategoryAndUser(int id) throws ServiceException {
        ArticleExtend byIdWithCategoryAndUser = extendMapper.findByIdWithCategoryAndUser(id);
        return byIdWithCategoryAndUser;
    }
    //保存或更新咨询信息
    @Override
    public void saveOrUpdate(Article article) throws ServiceException {

        mapper.insertSelective(article);
    }

    @Override
    public PageInfo<ArticleExtend> pageQueryArticleWithCategoryAndUser(int pageNum, int pageSize,String title,Integer status,Integer categoryId) throws ServiceException {
        PageHelper.startPage(pageNum, pageSize, true);
        List<ArticleExtend> list = extendMapper.findByIdWithStatusAndTitle(categoryId,status,title);
        PageInfo<ArticleExtend> articlePageInfo = new PageInfo<>(list);
        return articlePageInfo;
    }
    //通过id删除资讯
    @Override
    public void deleteById(int id) throws ServiceException {
        Article article = mapper.selectByPrimaryKey(id);

        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ArticleExtend> pageQueryRecommend(int pageNum, int pageSize) throws ServiceException {
        return null;
    }

    //修改咨询信息的状态，状态：0-待审核，1-不通过，2-通过，3-推荐
    @Override
    public void changeStatus(Integer id, Integer status) throws ServiceException {
        extendMapper.changeStatus(id,status);
    }


}
